﻿// (c) Norbert Huffschmid
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.

using System.Windows;
using SpinDotters.Core;

namespace SpinDotters.Widgets
{
    /// <summary>
    /// Clock base control
    /// </summary>
    [TemplateVisualState(Name = "Pulsing", GroupName = "PulseStates")]
    [TemplateVisualState(Name = "NonPulsing", GroupName = "PulseStates")]
    public class ClockBase : SpinningControlContainer
    {
        private const bool PULSE_DEFAULT = true;


        /// <summary>
        /// Pulse dependency property.
        /// </summary>
        public static readonly DependencyProperty PulseProperty =
            DependencyProperty.Register(
                "Pulse",
                typeof(bool),
                typeof(ClockBase),
                new PropertyMetadata(PULSE_DEFAULT, new PropertyChangedCallback(OnPulseChanged))
                );

        /// <summary>
        /// Gets or sets the pulse indicator.
        /// </summary>
        public bool Pulse
        {
            get { return (bool)GetValue(PulseProperty); }
            set { SetValue(PulseProperty, value); }
        }


        public ClockBase()
        {
            this.DefaultStyleKey = typeof(ClockBase);

            this.Loaded += new RoutedEventHandler(ClockBase_Loaded);
        }

        private void ClockBase_Loaded(object sender, RoutedEventArgs e)
        {
            OnPulseChanged(this, new DependencyPropertyChangedEventArgs());
        }


        private static void OnPulseChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ClockBase clockBase = (ClockBase)o;

            if (clockBase != null)
            {
                if (clockBase.Pulse)
                    VisualStateManager.GoToState(clockBase, "Pulsing", true);
                else
                    VisualStateManager.GoToState(clockBase, "NonPulsing", true);
            }
        }
    }
}
